package com.leetcode;

import com.leetcode.common.TreeNode;

/**
 * 98. 验证二叉搜索树
 * 中序遍历, 递归写法
 *
 * @author fy
 * @date 2022/4/26 14:07
 */
public class Solution98_3 {

    private Long inOrder = -Long.MIN_VALUE;
    public boolean isValidBST(TreeNode root) {
        return inOrder(root);
    }

    private boolean inOrder(TreeNode node) {
        if (node == null) {
            return true;
        }
        if (!inOrder(node.left)) {
            return false;
        }
        if (inOrder >= node.val) {
            return false;
        }
        inOrder = (long) node.val;
        if (!inOrder(node.right)) {
            return false;
        }
        return true;
    }

}
